﻿using System;
using NUnit.Framework;

namespace Ucto.ToExcelConvertor.Tests
{
	[TestFixture]
	public class DataReaderFixture : BaseFixture
	{
		[Test]
		public void ReadHeader()
		{
			// arrange
			var dataReader = this.Factory.GetDataReader();
			DataHeader header = null;

			// act
			try
			{
				header = dataReader.GetHeader();
			}
			finally
			{
				dataReader.Dispose();
			}

			// assert
			Assert.That(header, Is.Not.Null);
			Assert.That(header.ColumnCount, Is.EqualTo(MockResources.ColumnCount));
			Assert.That(header.RowCount, Is.EqualTo(MockResources.RowCount));
			Assert.That(header.ValueLenght, Is.EqualTo(MockResources.ValueLenght));
		}

		[Test]
		public void ReadHeaderTwice()
		{
			// arrange
			var dataReader = this.Factory.GetDataReader();
			DataHeader header = null;

			// act
			try
			{
				header = dataReader.GetHeader();
				header = dataReader.GetHeader();
			}
			finally
			{
				dataReader.Dispose();
			}

			// assert
			Assert.That(header, Is.Not.Null);
			Assert.That(header.ColumnCount, Is.EqualTo(MockResources.ColumnCount));
			Assert.That(header.RowCount, Is.EqualTo(MockResources.RowCount));
			Assert.That(header.ValueLenght, Is.EqualTo(MockResources.ValueLenght));
		}

		[Test]
		public void ReadValue()
		{
			// arrange
			var dataReader = this.Factory.GetDataReader();
			string value = null;

			// act
			try
			{
				value = dataReader.GetNextValue();
			}
			finally
			{
				dataReader.Dispose();
			}

			// assert
			Assert.That(value, Is.Not.Null);
			Assert.That(value, Is.EqualTo(MockResources.FirstValue));
		}

		[Test]
		public void ReadTwoValues()
		{
			// arrange
			var dataReader = this.Factory.GetDataReader();
			string value = null;

			// act
			try
			{
				value = dataReader.GetNextValue();
				value = dataReader.GetNextValue();
			}
			finally
			{
				dataReader.Dispose();
			}

			// assert
			Assert.That(value, Is.Not.Null);
			Assert.That(value, Is.EqualTo(MockResources.SecondValue));
		}

		[Test]
		public void ReadTillEnd()
		{
			// arrange
			var dataReader = this.Factory.GetDataReader();
			var header = dataReader.GetHeader();
			
			int count = 0;
			int expectedCount = header.RowCount * header.ColumnCount;

			// act
			try
			{
				while (!string.IsNullOrEmpty(dataReader.GetNextValue())) { count++; };
			}
			finally
			{
				dataReader.Dispose();
			}

			// assert
			Assert.That(count, Is.EqualTo(expectedCount));
		}

	}
}
